package com.xjlsoft.weather.task;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xjlsoft.weather.common.util.DateUtil;
import com.xjlsoft.weather.config.ApplicationContextProvider;
import com.xjlsoft.weather.enums.AvgFlagEnum;
import com.xjlsoft.weather.mapper.AerosolDataMapper;
import com.xjlsoft.weather.mapper.GroundDataMapper;
import com.xjlsoft.weather.mapper.RadiationDataMapper;
import com.xjlsoft.weather.service.AerosolAvgService;
import com.xjlsoft.weather.service.GroundAvgService;
import com.xjlsoft.weather.service.RadiationAvgService;
import com.xjlsoft.weather.service.WeatherTaskJob;
import com.xjlsoft.weather.util.WeatherDataUtil;

/**
 * 
 *
 */
public class AvgDayTask implements WeatherTaskJob {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AvgDayTask.class);

	private AerosolDataMapper aerosolDataMapper;

	private AerosolAvgService aerosolAvgService;

	private GroundDataMapper groundDataMapper;

	private GroundAvgService groundAvgService;

	private RadiationDataMapper radiationDataMapper;

	private RadiationAvgService radiationAvgService;

	/**
	 * 处理地面数据
	 */
	@Override
	public void run() {
		logger.info(">>>>>>>>>>>>开始 产生   大气平均 数据           AerosolAvgTask   当前线程名称 {}",
				Thread.currentThread().getName());

		aerosolDataMapper = ApplicationContextProvider.getBean(AerosolDataMapper.class);

		aerosolAvgService = ApplicationContextProvider.getBean(AerosolAvgService.class);

		groundDataMapper = ApplicationContextProvider.getBean(GroundDataMapper.class);

		groundAvgService = ApplicationContextProvider.getBean(GroundAvgService.class);

		radiationDataMapper = ApplicationContextProvider.getBean(RadiationDataMapper.class);

		radiationAvgService = ApplicationContextProvider.getBean(RadiationAvgService.class);

		// 查询 需要产生 日平均 数据 的原生 数据

		logger.info(">>>>>>>>>>>>开始   产生大气日平均 数据  当前线程名称 {}", Thread.currentThread().getName());
		List<String> listMinDatatime = aerosolDataMapper.getMinDataTime(AvgFlagEnum.FALSE.getValue());

		List<String> listMaxDatatime = aerosolDataMapper.getMaxDataTime(AvgFlagEnum.FALSE.getValue());
		if (listMinDatatime != null && listMaxDatatime != null) {
			if (listMinDatatime.size() == 1 && listMaxDatatime.size() == 1) {

				String minTime = listMinDatatime.get(0);
				String maxTime = listMaxDatatime.get(0);

				Date dateStart = DateUtil.StringToDate(minTime, "yyyy-MM-dd HH:mm:ss");
				Date dateEnd = DateUtil.StringToDate(maxTime, "yyyy-MM-dd HH:mm:ss");

				if (minTime.equals(maxTime)) {
					dateEnd = WeatherDataUtil.calculateEndDate(dateEnd, 1);
				}
				int result = aerosolAvgService.saveAvgDay(dateStart, dateEnd);
				logger.info("=====本次保存 大气日平均 数据=" + result + "条");
			}
		}
		logger.info(">>>>>>>>>>>>结束   产生大气日平均 数据  当前线程名称 {}", Thread.currentThread().getName());

		// 查询 需要产生 日平均 数据 的原生 数据
		logger.info(">>>>>>>>>>>>结束   产生地面日平均 数据  当前线程名称 {}", Thread.currentThread().getName());
		List<String> listMinDatatimeGround = groundDataMapper.getMinDataTime(AvgFlagEnum.FALSE.getValue());
		List<String> listMaxDatatimeGround = groundDataMapper.getMaxDataTime(AvgFlagEnum.FALSE.getValue());
		if (listMinDatatimeGround != null && listMaxDatatimeGround != null) {
			if (listMinDatatimeGround.size() == 1 && listMaxDatatimeGround.size() == 1) {

				String minTime = listMinDatatimeGround.get(0);
				String maxTime = listMaxDatatimeGround.get(0);

				Date dateStart = DateUtil.StringToDate(minTime, "yyyy-MM-dd HH:mm:ss");
				Date dateEnd = DateUtil.StringToDate(maxTime, "yyyy-MM-dd HH:mm:ss");

				if (minTime.equals(maxTime)) {
					dateEnd = WeatherDataUtil.calculateEndDate(dateEnd, 1);
				}
				int result = groundAvgService.saveAvgDay(dateStart, dateEnd);
				logger.info("=====本次保存 地面 日平均 数据=" + result);
			}

		}
		logger.info(">>>>>>>>>>>>结束   产生地面日平均 数据  当前线程名称 {}", Thread.currentThread().getName());

		// 查询 需要产生 日平均 数据 的原生 数据

		logger.info(">>>>>>>>>>>>开始  产生辐射日平均 数据  当前线程名称 {}", Thread.currentThread().getName());
		List<String> listMinDatatimeRadiation = radiationDataMapper.getMinDataTime(AvgFlagEnum.FALSE.getValue());
		List<String> listMaxDatatimeRadiation = radiationDataMapper.getMaxDataTime(AvgFlagEnum.FALSE.getValue());
		if (listMinDatatimeRadiation != null && listMaxDatatimeRadiation != null) {
			if (listMinDatatimeRadiation.size() == 1 && listMaxDatatimeRadiation.size() == 1) {

				String minTime = listMinDatatimeRadiation.get(0);
				String maxTime = listMaxDatatimeRadiation.get(0);

				Date dateStart = DateUtil.StringToDate(minTime, "yyyy-MM-dd HH:mm:ss");
				Date dateEnd = DateUtil.StringToDate(maxTime, "yyyy-MM-dd HH:mm:ss");

				if (minTime.equals(maxTime)) {
					dateEnd = WeatherDataUtil.calculateEndDate(dateEnd, 1);
				}
				int result = radiationAvgService.saveAvgDay(dateStart, dateEnd);
				logger.info("=====本次保存 辐射 日平均 数据=" + result);
			}

		}
		logger.info(">>>>>>>>>>>>结束   产生辐射日平均 数据  当前线程名称 {}", Thread.currentThread().getName());

	}
}